// 请求拦截器
function requestInterceptor() {
  (config) => {
    // do something before request is sent

    if (sessionStorage.getItem("qqqq-token")) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers["qqqq-Token"] = sessionStorage.getItem("qqqq-token");
    }
    return config;
  },
    (error) => {
      // do something with request error
      console.log(error); // for debug
      return Promise.reject(error);
    };
}

// 响应拦截器
function responseInterceptor() {
  (response) => {
    const res = response.data;
    // if the custom code is not 20000, it is judged as an error.
    if (res.code !== 20000) {
      ElMessage({
        message: res.message || "Error",
        type: "error",
        duration: 5 * 1000,
      });

      // 50008: Illegal token; 50012: Other clients logged in;
    }
    return res;
  };
}

export default { requestInterceptor, responseInterceptor };
